﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericTools;

namespace ReportingManagement
{
    public partial class MessageHistory : Form
    {
        public MessageHistory(string phoneNumber)
        {
            InitializeComponent();

            DataTable dataTable = Utilities.GetSMSUser(phoneNumber);
            if (dataTable.Rows.Count == 1)
            {
                DataRow row = dataTable.Rows[0];
                string name = row["Name"].ToString();
                this.Text = String.Format("Message History - {0} [{1}]", name, phoneNumber);
            }
            else
            {
                this.Text = String.Format("Message History - Unregistered User [{0}]", phoneNumber);
            }

            dataTable = Utilities.GetMessageHistory(phoneNumber);
            int sentMessages = 0;
            int receivedMessages = 0;
            int errorMessages = Utilities.GetErrorMessageCount(phoneNumber);
            DateTime maxDate = DateTime.Parse("2000-01-01");    //Just some very long time, but not quite long.
            DateTime minDate = DateTime.Now;
            foreach (DataRow r in dataTable.Rows)
            {
                bool sentToUser = Boolean.Parse(r["Received"].ToString());
                if (sentToUser) sentMessages++;
                else receivedMessages++;

                DateTime date = DateTime.Parse(r["Date"].ToString());
                if (date < minDate) minDate = date;
                if (date > maxDate) maxDate = date;
            }
            
            dataGridView1.DataSource = dataTable;
            Font readableFont = new Font("Bookman Old Style", 11);
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                bool receivedByUser;
                if (bool.TryParse(r.Cells["Received"].Value.ToString(), out receivedByUser))
                {
                    if (!receivedByUser)
                    {
                        DataGridViewCell cell = r.Cells["Message"];
                        cell.Style = new DataGridViewCellStyle()
                        {
                            BackColor = Color.White,
                            Font = (Font)readableFont.Clone(),
                            ForeColor = Color.Blue
                        };
                    }
                }
            }

            dataTable.Columns["Received"].ColumnName = "YES If received by user";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            int totalMessages = sentMessages + receivedMessages;
            int correctMessages = receivedMessages - errorMessages;
            decimal accuracy = 1;
            decimal dCorrectMessages = correctMessages;
            decimal dReceivedMessages = receivedMessages;
            if (receivedMessages != 0) accuracy = dCorrectMessages / dReceivedMessages;

            lblMessageSent.Text = sentMessages.ToString();
            lblMessageReceived.Text = receivedMessages.ToString();
            lblTotalMessages.Text = totalMessages.ToString();
            lblCorrectMessages.Text = correctMessages.ToString();
            lblErrorMessages.Text = errorMessages.ToString();
            lblAccuracy.Text = String.Format("Less than {0:P0}", accuracy);
            lblStartDate.Text = minDate.ToString();
            lblLastDate.Text = maxDate.ToString();
        }
    }
}
